/*
 * MoveMilitaryView.java
 *
 * @author Michael Hansen
 * @author Lukas Thuernagel
 * @author Soeren Warszus
 */

package risk.game;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.Hashtable;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class MoveMilitaryView extends JPanel{
    
    /**
     * Das zum View gehoerige Model
     */
    private MoveMilitaryModel moveMilitaryModel;
    
    /**
     * Der FrontController, der auf Action wartet
     */
    private FrontController frontController;
    
    /**
     * Territory aus dem Truppen abgezogen werden
     */
    private Territory fromTerritory;
           
    /**
     * Territory in das Truppen geschoben werden sollen
     */     
    private Territory toTerritory;
    
    /**
     * JSlider, aud dem der Wert eingestellt wird
     */
    private static JSlider slider;
    
    /**
     * Anzeige des Wertes des JSliders
     */
    private static JLabel newMilitary;
    
    /**
     * JLabel fuer das Land, aus dem Truppen abgezogen werden
     */
    private JLabel territoryFrom;
    
    /**
     * JLabel fuer das Land, in das Truppen gezogen werden
     */
    private JLabel territoryTo;
    
    /**
     * Anzahl der Bewegten Truppen
     */
    private static int moveTrupps;
    
    
    /** Creates a new instance of MoveMilitaryView */
    public MoveMilitaryView(MoveMilitaryModel mmm, FrontController fc) {
        this.moveMilitaryModel = mmm;
        this.frontController = fc;
        this.moveMilitaryModel.registerView(this);
    }
    
    /**
     * View wird initialisiert
     */
    public void initializeView(){
        GridBagLayout gbl      = new GridBagLayout();
        GridBagConstraints gbc = new GridBagConstraints();

        int columnWidths[] = {80, 20};
        int rowHeights[] = {48, 22};
        gbl.columnWidths = columnWidths;
        gbl.rowHeights = rowHeights;
        this.setLayout(gbl);
        
        territoryFrom = new JLabel(this.fromTerritory.getName());
        gbc.gridx = 0;
	gbc.gridy = 1;
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.anchor = GridBagConstraints.WEST;
        gbl.setConstraints(territoryFrom, gbc);
        this.add(territoryFrom);
        
        this.moveTrupps = (this.fromTerritory.getArmySize()-1)/2;
        newMilitary = new JLabel();
        newMilitary.setText("Anzahl der Bewegten Truppen: "+moveTrupps);
        gbc.gridx = 2;
	gbc.gridy = 0;
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.anchor = GridBagConstraints.WEST;
        gbl.setConstraints(newMilitary, gbc);
        this.add(newMilitary);
        
        JSlider slider = new JSlider(0, (this.fromTerritory.getArmySize()-1),(this.fromTerritory.getArmySize()-1)/2);
        slider.setBorder(BorderFactory.createTitledBorder("Wählen Sie die Truppenanzahl."));
        slider.setMinorTickSpacing(1);
        slider.setMajorTickSpacing(5);
        slider.setPaintTicks(true);
        slider.setPaintLabels(true);
        //Hashtable labelTable = new Hashtable();
        //labelTable.put(new Integer(this.fromTerritory.getArmySize()/2), new JLabel("Hälfte"));
        //labelTable.put(new Integer(this.fromTerritory.getArmySize()), new JLabel("Alle"));
        //slider.setLabelTable(labelTable);
        slider.addChangeListener(
                new javax.swing.event.ChangeListener() {
            public void stateChanged(ChangeEvent e) { 
                MoveMilitaryView.sliderStateChanged(e);
            }
        });
        gbc.gridx = 1;
	gbc.gridy = 1;
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.anchor = GridBagConstraints.WEST;
        gbl.setConstraints(slider, gbc);
        this.add(slider);
        
        territoryTo = new JLabel(this.toTerritory.getName());
        gbc.gridx = 2;
	gbc.gridy = 1;
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.anchor = GridBagConstraints.WEST;
        gbl.setConstraints(territoryTo, gbc);
        this.add(territoryTo);
        
        JButton okButton = new JButton();
        okButton.setActionCommand("moveMilitary");
        okButton.addActionListener(this.frontController);
        okButton.setText("OK");
        okButton.setSize(20,20);
        gbc.gridx = 2;
	gbc.gridy = 2;
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.anchor = GridBagConstraints.WEST;
        gbl.setConstraints(okButton , gbc);
        this.add(okButton );
        
    }
    
    /**
     * Registrierung der Veraenderung beim JSlider
     */
    public static void sliderStateChanged(ChangeEvent e){
        JSlider source = (JSlider)e.getSource();
        if (!source.getValueIsAdjusting()) {
            moveTrupps = (int)source.getValue();
            newMilitary.setText("Anzahl der Bewegten Truppen: "+new Integer(moveTrupps).toString());
        }else{
           
            //System.out.println(new Integer((int)source.getValue()).toString());
        }
    }
    
    /**
     * Setzen der beiden Laender aus dem bzw in das Truppen bewegt werden sollen
     *
     * @param Territory from Land, aus dem Truppen geholt werden.
     * @param Territory to   Land, in das Truppen geschickt werden
     */
    public void setTerritories(Territory from, Territory to){
        this.fromTerritory = from;
        this.toTerritory = to;
        initializeView();
    }
    
    public int getMovedMilitary(){
        return this.moveTrupps;
    }
    
    /**
     * Dynamischen Inhalt des Panels loeschen
     */
    public void reset(){
        //this.territoryTo.removeAll();
        //this.territoryFrom.removeAll();
        this.removeAll();
    }
}
